package com.jumi.microservice.material.controller;

import com.github.pagehelper.PageInfo;
import com.jumi.common.log.annotation.Log;
import com.jumi.common.log.enums.BusinessType;
import com.jumi.microservice.common.base.AjaxResult;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.material.domain.JmMaterialSmallUnit;
import com.jumi.microservice.material.service.JmMaterialSmallUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 商品最小单位 前端控制器
 * </p>
 *
 * @author ZhangY
 */
@Api(tags = {"【基础资料】商品单位信息接口"})
@RestController
@RequestMapping("/smunit")
@CrossOrigin
@Slf4j
public class JmMaterialSmallUnitController extends BaseController
{

	@Autowired
	private JmMaterialSmallUnitService jmMaterialSmallUnitService;

	/**
	 * @Description: 条件+分页查询
	 * @Param: [page, size, smunitName, smunitStatus, beginTime, endTime]
	 * @Return: com.jumi.microservice.common.core.web.page.TableDataInfo
	 */
	@ApiOperation("条件查询+分页")
	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	public TableDataInfo findAll(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String smunitName, Integer smunitStatus, Date beginTime, Date endTime)
	{

		PageInfo<JmMaterialSmallUnit> smunitPageInfo = jmMaterialSmallUnitService.findAll(pageNum, pageSize, smunitName, smunitStatus, beginTime, endTime);
		return new TableDataInfo<>(smunitPageInfo.getList(), (int) smunitPageInfo.getTotal());
	}

	/**
	 * @Description: 根据Id查询
	 * @Param: [smunitId]
	 * @Return: com.jumi.microservice.common.core.web.domain.AjaxResult
	 */
	@ApiOperation("根据Id查询")
	@ApiImplicitParam(name = "attrValueId", value = "id值", required = true)
	@RequestMapping(value = "/{smunitId}", method = RequestMethod.GET)
	public AjaxResult findById(@PathVariable Integer smunitId)
	{
		JmMaterialSmallUnit smunit = jmMaterialSmallUnitService.findById(smunitId);
		return AjaxResult.success(smunit);
	}

	/**
	 * @Description: 添加一条
	 * @Param: [smunit]
	 * @Return: com.jumi.microservice.common.core.web.domain.AjaxResult
	 */
	@Log(title = "供应链管理-基础管理-最小单位",businessType = BusinessType.INSERT)
	@ApiOperation("增加")
	@RequestMapping(method = RequestMethod.POST)
	public AjaxResult add(@RequestBody @Validated JmMaterialSmallUnit smunit, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy)
	{
		smunit.setCreateBy(createBy);
		int result = jmMaterialSmallUnitService.add(smunit);
		if (result == 0)
		{
			return AjaxResult.error("已存在相同名称");
		}
		return AjaxResult.success(smunit.getSmunitId());
	}

	/**
	 * @Description: 修改一条
	 * @Param: [smunit]
	 * @Return: com.jumi.microservice.common.core.web.domain.AjaxResult
	 */
	@Log(title = "供应链管理-基础管理-最小单位",businessType = BusinessType.UPDATE)
	@ApiOperation("修改一条")
	@RequestMapping(method = RequestMethod.PUT)
	public AjaxResult update(@RequestBody @Validated JmMaterialSmallUnit smunit, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy)
	{
		smunit.setUpdateBy(updateBy);
		int result = jmMaterialSmallUnitService.update(smunit);
		if (result == 0)
		{
			return AjaxResult.error("已存在相同名称");
		}
		return AjaxResult.success(smunit.getSmunitId());
	}

	/**
	 * @Description: 批量删除
	 * @Param: [smunitId]
	 * @Return: com.jumi.microservice.common.core.web.domain.AjaxResult
	 */
	@Log(title = "供应链管理-基础管理-最小单位",businessType = BusinessType.DELETE)
	@ApiOperation("批量删除")
	@RequestMapping(method = RequestMethod.DELETE)
	public AjaxResult deleteById(String ids, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy)
	{
		String[] smIds = ids.split(",");
		int result = jmMaterialSmallUnitService.deleteByIds(smIds, updateBy);
		if (result == 0)
		{
			return AjaxResult.error("启动状态不能删除");
		}
		return AjaxResult.success();
	}

	/**
	 * @Description: 根据Id修改状态
	 * @Param: [smunitId, smunitStatus]
	 * @Return: com.jumi.microservice.common.core.web.domain.AjaxResult
	 */
	@Log(title = "供应链管理-基础管理-最小单位",businessType = BusinessType.UPDATE)
	@ApiOperation("根据Id修改状态")
	@RequestMapping(value = "/setStatus/{smunitId}/{smunitStatus}", method = RequestMethod.PUT)
	public AjaxResult setStatus(@PathVariable Integer smunitId, @PathVariable Integer smunitStatus, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy)
	{
		int result = jmMaterialSmallUnitService.setStatus(smunitId, smunitStatus, updateBy);
		return AjaxResult.success(smunitId);
	}
}
